
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Instrumenting Log4j | Metrics</title>
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css"/>
    <link rel="stylesheet" href="../_static/metrics.css" type="text/css"/>
    <link rel="top" title="Metrics" href="../index.html"/>
    <link rel="up" title="User Manual" href="index.html"/>
    <link rel="next" title="Instrumenting Logback" href="logback.html"/>
    <link rel="prev" title="Instrumenting Jetty" href="jetty.html"/>
    <style lang="text/css">
        #top-bar, #top-bar small, #top-bar a {
            text-shadow: 0px -1px 0px #cf2c0f;
            color: #fff;
        }
        
        #top-bar {
            background-color: #ED4A2D;
            background-image: -moz-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -ms-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ff684b), to(#cf2c0f));
            background-image: -webkit-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -o-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: linear-gradient(top, #ff684b, #cf2c0f);
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ff684b', endColorstr = '#cf2c0f', GradientType = 0);
            -webkit-border-radius: 0;
            -moz-border-radius: 0;
            border-radius: 0;
        }


        .hero-unit {
            background-image: url("../_static/metrics-hat.png") !important;
            background-repeat: no-repeat !important;
            background-position: 30px 50px;
        }

        .hero-unit div.section {
            padding-left: 200px !important;
        }
    </style>
</head>
<body>
    <a href="https://github.com/dropwizard/metrics">
        <img style="position: absolute; top: 0; right: 0; border: 0;"
             src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"
             alt="Fork me on GitHub"></a>
    <div class="navbar">
        <div class="navbar-inner container-fluid" id="top-bar">
            <header class="row-fluid">
                <h1 class="span12" id="title"><img class="logo" src="../_static/metrics-logo.png" alt="Logo"/>
                    <a href="../index.html">Metrics</a>
                    <small>Mind the gap.</small>
                </h1>
            </header>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span3" id="sidebar">
                <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../getting-started.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="core.html">Metrics Core</a></li>
<li class="toctree-l2"><a class="reference internal" href="healthchecks.html">Health Checks</a></li>
<li class="toctree-l2"><a class="reference internal" href="ehcache.html">Instrumenting Ehcache</a></li>
<li class="toctree-l2"><a class="reference internal" href="ganglia.html">Reporting to Ganglia</a></li>
<li class="toctree-l2"><a class="reference internal" href="graphite.html">Reporting to Graphite</a></li>
<li class="toctree-l2"><a class="reference internal" href="httpclient.html">Instrumenting Apache HttpClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="jdbi.html">Instrumenting JDBI</a></li>
<li class="toctree-l2"><a class="reference internal" href="jersey.html">Instrumenting Jersey 1.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="jersey.html#instrumenting-jersey-2-x">Instrumenting Jersey 2.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="jetty.html">Instrumenting Jetty</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Instrumenting Log4j</a></li>
<li class="toctree-l2"><a class="reference internal" href="logback.html">Instrumenting Logback</a></li>
<li class="toctree-l2"><a class="reference internal" href="jvm.html">JVM Instrumentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html">JSON Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlets.html">Metrics Servlets</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlet.html">Instrumenting Web Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="third-party.html">Third Party Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../about/index.html">About Metrics</a></li>
</ul>

                <hr/>
                <ul>
                    <li><a href="https://dropwizard.github.io/metrics/3.2.2/apidocs/">Javadoc</a></li>
                    <li><a href="https://groups.google.com/forum/#!forum/metrics-user">Mailing List</a></li>
                </ul>
            </div>
            <div class="span9" id="body">
                
  <div class="section" id="instrumenting-log4j">
<span id="manual-log4j"></span><h1>Instrumenting Log4j</h1>
<p>The <code class="docutils literal"><span class="pre">metrics-log4j</span></code> and <code class="docutils literal"><span class="pre">metrics-log4j2</span></code> modules provide <code class="docutils literal"><span class="pre">InstrumentedAppender</span></code>, a Log4j <code class="docutils literal"><span class="pre">Appender</span></code> implementation
(for log4j 1.x and log4j 2.x correspondingly) which records the rate of logged events by their logging level.</p>
<p>You can add it to the root logger programmatically.</p>
<p>For log4j 1.x:</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">InstrumentedAppender</span> <span class="n">appender</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InstrumentedAppender</span><span class="o">(</span><span class="n">registry</span><span class="o">);</span>
<span class="n">appender</span><span class="o">.</span><span class="na">activateOptions</span><span class="o">();</span>
<span class="n">LogManager</span><span class="o">.</span><span class="na">getRootLogger</span><span class="o">().</span><span class="na">addAppender</span><span class="o">(</span><span class="n">appender</span><span class="o">);</span>
</pre></div>
</div>
<p>For log4j 2.x:</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">Filter</span> <span class="n">filter</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>        <span class="c1">// That&#39;s fine if we don&#39;t use filters; https://logging.apache.org/log4j/2.x/manual/filters.html</span>
<span class="n">PatternLayout</span> <span class="n">layout</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span> <span class="c1">// The layout isn&#39;t used in InstrumentedAppender</span>

<span class="n">InstrumentedAppender</span> <span class="n">appender</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InstrumentedAppender</span><span class="o">(</span><span class="n">metrics</span><span class="o">,</span> <span class="n">filter</span><span class="o">,</span> <span class="n">layout</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
<span class="n">appender</span><span class="o">.</span><span class="na">start</span><span class="o">();</span>

<span class="n">LoggerContext</span> <span class="n">context</span> <span class="o">=</span> <span class="o">(</span><span class="n">LoggerContext</span><span class="o">)</span> <span class="n">LogManager</span><span class="o">.</span><span class="na">getContext</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
<span class="n">Configuration</span> <span class="n">config</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="na">getConfiguration</span><span class="o">();</span>
<span class="n">config</span><span class="o">.</span><span class="na">getLoggerConfig</span><span class="o">(</span><span class="n">LogManager</span><span class="o">.</span><span class="na">ROOT_LOGGER_NAME</span><span class="o">).</span><span class="na">addAppender</span><span class="o">(</span><span class="n">appender</span><span class="o">,</span> <span class="n">level</span><span class="o">,</span> <span class="n">filter</span><span class="o">);</span>
<span class="n">context</span><span class="o">.</span><span class="na">updateLoggers</span><span class="o">(</span><span class="n">config</span><span class="o">);</span>
</pre></div>
</div>
<p>You can also use standard log4j2 configuration, via plugin support:</p>
<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;Configuration</span> <span class="na">status=</span><span class="s">&quot;INFO&quot;</span> <span class="na">name=</span><span class="s">&quot;log4j2-config&quot;</span> <span class="na">packages=</span><span class="s">&quot;com.codahale.metrics.log4j2&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;Appenders&gt;</span>
    <span class="nt">&lt;MetricsAppender</span> <span class="na">name=</span><span class="s">&quot;metrics&quot;</span> <span class="na">registryName=</span><span class="s">&quot;shared-metrics-registry&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/Appenders&gt;</span>
<span class="nt">&lt;Loggers&gt;</span>
    <span class="nt">&lt;Root</span> <span class="na">level=</span><span class="s">&quot;INFO&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;AppenderRef</span> <span class="na">ref=</span><span class="s">&quot;metrics&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/Root&gt;</span>
<span class="nt">&lt;/Loggers&gt;</span>
<span class="nt">&lt;/Configuration&gt;</span>
</pre></div>
</div>
</div>


            </div></div>
        <hr/>
        <footer>
            <p style="float: left">
            &copy; Copyright 2010-2014, Coda Hale, Yammer Inc..
            Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>
            1.5.2.
            </p>
            <p style="float: right">Dropwizard Metrics v3.2.2</p>
        </footer>
    </div>
</body>
</html>


